package com.yzt.zhuju.widget.tuiview;

import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableContainer;
import android.graphics.drawable.StateListDrawable;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.drawable.DrawableCompat;

import com.yzt.p_base.AppUtils;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class DrawableUtils {

    private DrawableUtils() {
        throw new IllegalStateException();
    }

    /**
     * 着色
     */
    public static Drawable tint(@NonNull Drawable drawable, @ColorInt int color) {
        Drawable d = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTint(d, color);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }

    /**
     * 着色
     */
    public static Drawable tint(@NonNull Drawable drawable, @ColorInt int color, @NonNull PorterDuff.Mode mode) {
        Drawable d = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTintMode(d, mode);
        DrawableCompat.setTint(d, color);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }

    /**
     * 着色，注意states顺序
     */
    public static Drawable tint(@NonNull Drawable drawable, @NonNull int[][] states, @NonNull int[] colors) {
        if (states.length != colors.length) {
            throw new IllegalArgumentException("states length error");
        }
        StateListDrawable listDrawable = new StateListDrawable();
        ColorStateList colorStateList = new ColorStateList(states, colors);
        for (int[] state : states) {
            listDrawable.addState(state, drawable);
        }
        Drawable result = DrawableCompat.wrap(listDrawable);
        DrawableCompat.setTintList(result, colorStateList);
        result.setBounds(0, 0, result.getIntrinsicWidth(), result.getIntrinsicHeight());
        return result;
    }

    /**
     * StateListDrawable XML中定义SVG的兼容方式
     */
    public static Drawable createStateListDrawable(@DrawableRes int defaultRes,
                                                   @DrawableRes int pressedRes,
                                                   @DrawableRes int selectedRes) {
        StateListDrawable result = new StateListDrawable();
        if (defaultRes == 0) {
            return null;
        }
        if (selectedRes != 0) {
            result.addState(new int[]{android.R.attr.state_selected},
                    AppCompatResources.getDrawable(AppUtils.getApp(), selectedRes).mutate());
        }
        if (pressedRes != 0) {
            result.addState(new int[]{android.R.attr.state_pressed},
                    AppCompatResources.getDrawable(AppUtils.getApp(), pressedRes).mutate());
        }
        result.addState(new int[]{},
                AppCompatResources.getDrawable(AppUtils.getApp(), defaultRes).mutate());
        return result;
    }

    /**
     * StateListDrawable是否为空
     */
    public static boolean isStateListDrawableEmpty(@Nullable StateListDrawable drawable) {
        if (drawable == null) {
            return true;
        }
        DrawableContainer.DrawableContainerState state = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
        return state == null || state.getChildCount() < 1;
    }

}
